<template>
  <div class="hello">
    <home-header></home-header>
    <div class="swipe">
      <swiper :options="swiperOption" v-if="swipelist.length">
        <swiper-slide v-for="item in swipelist" :key="item.id">
          <img :src="item.imgUrl" alt="">
        </swiper-slide>
        <div class="swiper-pagination" slot="pagination"></div>
      </swiper>
    </div>
    <home-icons :list='iconlist'></home-icons>
    <home-recommend :list="recommendlist"></home-recommend>
    <home-weekend :list="weekendlist"></home-weekend>
  </div>
</template>

<script>
import Header from "./components/Header.vue";
import Icons from "./components/Icons";
import Recommend from "./components/IndexRecommend";
import Weekend from "./components/IndexWeekends";
import axios from "axios";
import{mapState} from 'vuex'
export default {
  name: "HelloWorld",
  data() {
    return {
      lastcity:'',
      swipelist: [],
      iconlist:[],
      recommendlist:[],
      weekendlist:[],
      swiperOption: {
        speed: 1000,
        pagination: ".swiper-pagination",
        loop: true,
        autoplay: 3000
      }
    };
  },
  computed:{
    ...mapState(['city'])
  },
  components: {
    homeHeader: Header,
    homeIcons: Icons,
    homeRecommend: Recommend,
    homeWeekend: Weekend
  },
  methods: {
    getHomeInfo() {
      axios.get("/api/index.json?city="+this.city).then(this.suc);
    },
    suc(res) {
      var data = res.data.data;
      this.swipelist = data.swiperList;
      this.iconlist=data.iconList;
      this.recommendlist=data.recommendList;
      this.weekendlist=data.weekendList;
      
      // console.log(res)
    }
  },
  mounted() {
    this.lastcity=this.city;
    this.getHomeInfo();
  },
  activated(){
    if(this.lastcity!=this.city){
      this.lastcity=this.city;
      this.getHomeInfo();
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.swipe {
  /* height: 150px; */
  overflow: hidden;
  width: 100%;
  height: 0;
  padding-bottom: 37%;
}

.swipe img {
  width: 100%;
  height: 150px;
}
</style>
